package net.sxnic.workflow.whimpl;

import net.sxnic.ugr.context.AppContext;
import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.handler.AbstractHandler;
import net.sxnic.workflow.handler.HandlerExt;
import net.sxnic.workflow.handler.WfHandlerException;
import net.sxnic.workflow.task.Task;
import net.sxnic.workflow.wfentity.WfEntity;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CreateTaskForSelfHandler extends AbstractHandler<WfEntity> implements HandlerExt<WfEntity> {

	private static Logger logger = LoggerFactory.getLogger(CreateTaskForSelfHandler.class);

	public CreateTaskForSelfHandler(WfContextImpl<WfEntity> context) {
		super(context);
	}

	@Override
	public void editExecute() throws WfHandlerException {

	}

	@Override
	public void saveExecute() throws WfHandlerException {

		if (StringUtils.isNotBlank(context.getTaskId())) {
			logger.debug("==task=existed==CreateTaskForSelfHandler==ignore===taskId=" + context.getTaskId());
			return;
		}

		// 保存实体
		context.getWfenManager().save(context.getEntity());
		context.setEntityId(context.getEntity().getId());

		logger.debug("==entity save success===id===" + context.getEntityId());

		Task task = context.getTaskManager().createSelfTaskatStartNode(context.getEntityId(),
				 context.getProcessName(), AppContext.getUserName());

		context.setTask(task);
		context.setTaskId(task.getId());

		logger.debug("=====create==self==task==success===" + context.getTask());
	}

	@Override
	public void viewExecute() throws WfHandlerException {
	}

	@Override
	public void submitExecute() throws WfHandlerException {

	}

}
